package com.example.curvedemo.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.viewpager.widget.ViewPager;

import com.example.curvedemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 新版曲线图带提示
 */
public class CurveNewTipView extends LinearLayout {

    private XFlowLayout flow_curve_tip;
    private CurveNewView curve_curve_tip;

    private Context context;

    private List<ItemData> tips = new ArrayList<>();

    /**
     * 是否展示曲线图上方的提示（默认展示）
     */
    private boolean showCurveTip = true;

    /**
     * 线颜色
     */
    private String[] lineColors = new String[]{"#52C41A", "#FFAD0D", "#307DFF", "#7C48EE", "#23D4EC", "#F03D3D", "#FF7A25", "#A66262"};

    public CurveNewTipView(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public CurveNewTipView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    /**
     * 初始化布局
     */
    @SuppressLint("MissingInflatedId")
    public void initView() {
        // 加载布局
        View view = LayoutInflater.from(context).inflate(R.layout.view_curve_new_tip, this);
        flow_curve_tip = view.findViewById(R.id.flow_curve_tip);
        curve_curve_tip = view.findViewById(R.id.curve_curve_tip);
    }

    /**
     * 是否展示曲线图上方的提示（默认展示）
     *
     * @param showCurveTip 是否展示
     */
    public void setShowCurveTip(boolean showCurveTip) {
        this.showCurveTip = showCurveTip;
    }

    /**
     * 设置是否需要绘制下标
     *
     * @param bomTip
     */
    public void setBomTip(boolean bomTip) {
        curve_curve_tip.setBomTip(bomTip);
    }

    /**
     * 是否绘制Y轴刻度线
     *
     * @param scaleLine
     */
    public void setScaleLine(boolean scaleLine) {
        curve_curve_tip.setScaleLine(scaleLine);
    }

    /**
     * 设置是否展示左边刻度
     *
     * @param leftTip
     */
    public void setLeftTip(boolean leftTip) {
        curve_curve_tip.setLeftTip(leftTip);
    }

    /**
     * 设置左边刻度对应的线（-1代表柱状图）
     *
     * @param leftScaleNum
     */
    public void setLeftScaleNum(int leftScaleNum) {
        curve_curve_tip.setLeftScaleNum(leftScaleNum);
    }

    /**
     * 设置NestedScrollView滑动点击兼容
     *
     * @param scrollView
     */
    public void setScrollView(NestedScrollView scrollView) {
        curve_curve_tip.setScrollView(scrollView);
    }

    /**
     * 设置ViewPager滑动点击兼容
     *
     * @param viewPager
     */
    public void setViewPager(ViewPager viewPager) {
        curve_curve_tip.setViewPager(viewPager);
    }


    /**
     * 设置点击监听
     *
     * @param itemOclickListener
     */
    public void setItemOclickListener(CurveNewView.ItemOclickListener itemOclickListener) {
        curve_curve_tip.setItemOclickListener(itemOclickListener);
    }


    /**
     * 设置数据（柱状图且带顶部提示数据）
     *
     * @param xDataList
     * @param lineDataList
     * @param columnarDataList
     * @param lineTopDataList
     * @param numY
     */
    public void setData(List<CurveNewData.XData> xDataList, List<CurveNewData.LineData> lineDataList, List<CurveNewData.LineData> columnarDataList, List<CurveNewData.LineData> lineTopDataList, int numY) {
        if (curve_curve_tip.getWidth() == 0) {
            curve_curve_tip.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    curve_curve_tip.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    curve_curve_tip.setData(xDataList, lineDataList, columnarDataList, lineTopDataList, numY);
                }
            });
        } else {
            curve_curve_tip.setData(xDataList, lineDataList, columnarDataList, lineTopDataList, numY);
        }
        tips.clear();
        for (int i = 0; i < lineDataList.size(); i++) {
            CurveNewData.LineData bean = lineDataList.get(i);
            String lineColor;
            //数据超出预设颜色就设置透明看不见
            if (i < lineColors.length) {
                lineColor = lineColors[i];
            } else {
                lineColor = "#00000000";
            }
            tips.add(new ItemData(lineColor, bean.getTitle()));
        }
        if (showCurveTip) {
            //只有一条线不展示提示
            if (tips.size() > 1) {
                flow_curve_tip.setVisibility(View.VISIBLE);
                XFlowLayout.Adapter adapter = new XFlowLayout.Adapter() {
                    @Override
                    public int getItemCount() {
                        return tips.size();
                    }

                    @Override
                    public View getItemViewByPos(XFlowLayout view, int pos) {
                        LinearLayout layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_curve_tip_content,
                                view, false);
                        TextView txt_item_curve_tip = layout.findViewById(R.id.txt_item_curve_tip);
                        ImageView img_item_curve_tip = layout.findViewById(R.id.img_item_curve_tip);
                        txt_item_curve_tip.setText(tips.get(pos).title);
                        img_item_curve_tip.setBackgroundColor(Color.parseColor(tips.get(pos).color));
                        return layout;
                    }
                };
                flow_curve_tip.setAdapter(adapter);
                adapter.notifyDataChanged();
            } else {
                flow_curve_tip.setVisibility(View.GONE);
            }
        } else {
            flow_curve_tip.setVisibility(View.GONE);
        }
    }

    /**
     * 设置数据
     *
     * @param xDataList    x轴数据
     * @param lineDataList 线数据
     * @param numY         几等分
     */
    public void setData(List<CurveNewData.XData> xDataList, List<CurveNewData.LineData> lineDataList, int numY) {
        setData(xDataList, lineDataList, null, numY);
    }

    /**
     * 设置数据（带顶部提示数据）
     *
     * @param xDataList       x轴数据
     * @param lineDataList    线数据
     * @param lineTopDataList 顶部提示
     * @param numY            几等分
     */
    public void setData(List<CurveNewData.XData> xDataList, List<CurveNewData.LineData> lineDataList, List<CurveNewData.LineData> lineTopDataList, int numY) {
        setData(xDataList, lineDataList, null, lineTopDataList, numY);
    }


    public static class ItemData {
        public String color, title;

        public ItemData(String color, String title) {
            this.color = color;
            this.title = title;
        }
    }

}
